package com.ttl.ittl.requirement;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.ttl.bc.stock.request2.db.BCStkIssue2DAO;

public class RequirementBO {

	@SuppressWarnings("unchecked")
	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		RequirementActionForm cform = (RequirementActionForm)form;		
		RequirementBean cbean = new RequirementBean();				
//##################		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try	{
			Date docDate = df.parse(cform.getDocDate());            					
			cbean.setDocDate(docDate);			
		} catch (ParseException e) {		
			e.printStackTrace();	
		}		
//##################		
		cbean.setDocNo(cform.getDocNo());
		cbean.setDepartCode(cform.getDepartCode());
		cbean.setPersonCode(cform.getPersonCode());
						
		try {
			cbean.setRequirement(new String(cform.getRequirement().getBytes("ISO8859_1"),"UTF-8"));
//			stkIssue2Form.setOrderNo(new String(request.getParameter("orderNo").getBytes("ISO8859_1"),"UTF-8"));
//			stkIssue2Form.setMyDescription(new String(request.getParameter("myDescription").getBytes("ISO8859_1"),"UTF-8"));
//			stkIssue2Form.setItemName1(new String(request.getParameter("itemName1").getBytes("ISO8859_1"),"UTF-8"));			
		}catch (Exception e) {
			
		}				
		RequirementDAO cdao = new RequirementDAO();
		boolean status = false;
		
		String hql = "FROM RequirementBean WHERE docNo = '"+cform.getDocNo()+"' ";				
		List al = cdao.find(hql);
						
		if(al == null) {
			status = cdao.add(cbean);
		}else {
			String maxDocNo = RequirementBO.getMaxDocNo("REQ");			
			cbean.setDocNo(maxDocNo);
			status = cdao.add(cbean);
		}		
													
		return status;
	}	
	
	@SuppressWarnings("unchecked")
	public List find() throws Exception {
		
		String hql = "FROM RequirementBean ORDER BY docNo desc";
		RequirementDAO dao = new RequirementDAO();
		List results = dao.find(hql);		
				
		
		return results;
	}	
		
	@SuppressWarnings("unchecked")
	public static String getMaxDocNo(String preDocNo) {
		Date date = new Date();
		
		SimpleDateFormat simpleYear = new SimpleDateFormat("yyyy");
		String yearStr = simpleYear.format(date);
		
		if(Integer.valueOf(yearStr)<2500) {
			int yearStr2 = Integer.valueOf(yearStr)+543;
			yearStr = (""+yearStr2).substring(2);
		} else {
			yearStr = yearStr.substring(2);
		}
		
		SimpleDateFormat simpleMonth = new SimpleDateFormat("MM");
		String monthStr = simpleMonth.format(date);
		
		String docNoStr = preDocNo+"-"+yearStr+monthStr;
		
		String hql = "SELECT MAX(docNo) FROM RequirementBean WHERE docNo like '"+docNoStr+"%' ";
				
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
						
		List result = dao.find(hql);		
		String docNo = null;
		
		String maxDocNo = null;
		if(result.get(0) != null) {
			
			docNo = result.get(0).toString();
			int temp = 1+Integer.valueOf(docNo.substring(docNo.length()-3,docNo.length()));			
			NumberFormat formatter = new DecimalFormat("000");
			docNo = formatter.format(temp);
					
			maxDocNo = docNoStr+docNo; 			
		}else {
			maxDocNo = docNoStr+"001";
		}
						
		return maxDocNo;
	}	
}
